package rest.feign;

import feign.Feign;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import rest.entity.User;

/**
 * Feign可以接受与rest服务器端相同的对象
 */
public class FeignStarter {

    public static void main(String[] args) {
        CallProvider target = Feign.builder().target(CallProvider.class, "http://localhost:7778");
        System.out.println(target.test());

        FeignStarter feignStarter = new FeignStarter();
        feignStarter.getUser();
        feignStarter.sendUser();
    }

    /**
     * 从rest获取对象
     */
    public void getUser(){
        //new GsonDecoder()为解码器，会将返回的json字符串转换为接口方法对应的对象
        CallProvider target = Feign.builder().decoder(new GsonDecoder()).target(CallProvider.class, "http://localhost:7778");
        User book = target.getUser("book");
        System.out.println("user id :" + book.getUserId());
        System.out.println("user name : " + book.getUserName());
    }

    /**
     * 发送对象到rest
     */
    public void sendUser(){
        //encoder(new GsonEncoder())为编码器，将对象转化为json字符串
        CallProvider callProvider = Feign.builder().encoder(new GsonEncoder()).target(CallProvider.class, "http://localhost:7778");
        User user = new User();
        user.setUserId("id");
        user.setUserName("雨柔子");
        String sendUser = callProvider.sendUser(user);
        System.out.println(sendUser);
    }
}
